<?php

namespace app\member\home;

use app\index\controller\User;
use think\Request;
#use think\json;
use app\member\model\MemberAddress;
use app\index\model\Region;
use app\index\model\GetCityProper as GetCityProperModel;
use think\Db;

class Address extends User
{
    //地址管理
    public function index()
    {
        $map['member_id']=$this->mid;
        //检索
        if(request()->isGet()){
            $name=input('get.name');
            $mobile=input('get.mobiles');
            $address=input('get.address');
            if(!empty($name)){
                $map['consignee']=array('like',"%$name%");
            }
            if(!empty($mobile)){
                $map['mobile|tel']=array('like',"%$mobile%");
            }
            if(!empty($address)){
                $map['address']=array('like',"%$address%");
            }
        }
        $map['is_del'] = 0;
        $res=Db::name('member_address')->where($map)->order('addr_id DESC')->paginate(10);
        $all_page = $res->lastPage();
        $this->assign('all_page',$all_page);
        $page = $res->render('上一页','下一页');
        $this->assign('page',$page);
        $res = $res->all();

        //获取所有区域
        $Region   = new Region;
        $all_city = $Region->getAll();
        foreach($res as $key=>$value){
            $res[$key]['p_name']=$all_city[$value['province']]['name'];
            $res[$key]['c_name']=$all_city[$value['city']]['name'];
            $res[$key]['a_name']=$all_city[$value['area']]['name'];
        }
        $this->assign('address',$res);
        return $this->fetch('index');
    }
    // 新增地址 sky
    public function create()
    {
        if (Request::instance()->isPost())
        {
            $post = Request::instance()->post();
            //判断默认收货地址
            if($post['is_default'] == 1){
                $this->setAllDefault();
            }
            $post['member_id'] = $this->mid;
            $addressModel = new MemberAddress();
            $last_id = $addressModel->createAddress($post);
            if($last_id){

                $result = array('error'=>0,'addr_id'=>$last_id);
            }else{
                $result = array('error'=>1);
            }
            return json($result);
        }

        // 省份
        $regionModel = new Region();
        //dump($regionModel);exit;
        $provinces = $regionModel->getRegion(0);
        $this->assign('provinces', $provinces);
        
        $this->view->engine->layout(false);

        //判断加载
        $fetch_type = '';
        $fetch_type = input('fetch_type');
        if($fetch_type == 'address_index'){
            return $this->fetch('_address');
        }else{
            return $this->fetch('index@trade/_addressLayer');
        }
        
    }

    //设置默认地址 sky
    public function setDefault()
    {
        $addrId = input('addrId', 0);
        if($this->mid==0) return false;
        $addressModel = new MemberAddress();
        $rs = $addressModel->setDefaultAddress($this->mid,$addrId);
        if($rs==1){
            $result = ['error'=>0];
        }else{
            $result = ['error'=>1];
        }
        return json($result);
    }

    //修改地址 sky
    public function edit()
    {
        $addrId = input('addrId', 0);
        if($this->mid==0) return false;
        
        if (Request::instance()->isPost())
        {
            $addressModel = new MemberAddress();
            // 过滤post数组中的非数据表字段数据

            //判断默认
            $default = 0;
            $default = input('is_default');
            if($default == 1){
                $this->setAllDefault();
            }
            $rs = $addressModel->allowField(true)->save($_POST,['addr_id' => $addrId]);

            if($rs==1 || $rs==0){
                $result = ['error'=>0];
            }else{
                $result = ['error'=>1];
            }
            return json($result);
        }

        $addressModel = new MemberAddress();
        $address = $addressModel->getOne($addrId);
        $this->assign('address', $address);

        // 省份
        $regionModel = new Region();
        $provinces = $regionModel->getRegion(0);
        $this->assign('provinces', $provinces);

        //市
        $city = $regionModel->getRegion($address['province']);
        $this->assign('city', $city);
        
        //区
        $area = $regionModel->getRegion($address['city']);
        $this->assign('area', $area);
        
        $this->view->engine->layout(false);

        //判断加载
        $fetch_type = '';
        $fetch_type = input('fetch_type');
        if($fetch_type == 'address_index'){
            return $this->fetch('_address');
        }else{
            return $this->fetch('index@trade/_addressLayer');
        }
    }

    public function search()
    {
        $mid = $this->mid;
        if($mid==0) return false;

        $wd = input('wd', '');
        if(empty($wd)) return false;

        $addressModel = new MemberAddress();
        $list = $addressModel->search($mid,$wd);
        $this->assign('addressList', $list); 

        $regionModel = new Region();
        $region = $regionModel->getAll();
        $this->assign('region', $region);

        $this->view->engine->layout(false);
        return $this->fetch('index@trade/_addressLi');
    }


    //修改所有默认地址
    public function setAllDefault()
    {
        $mid = $this->mid;
        $addressModel = new MemberAddress();
        $res = $addressModel->setAllDefault($mid);
        return $res;
    }

    //删除
    public function del()
    {
        $addrId = input('addrId', 0);
        if($this->mid==0) return false;
        $addressModel = new MemberAddress();
        $rs = $addressModel->del($this->mid,$addrId);
        if($rs==1){
            $result = ['error'=>0];
        }else{
            $result = ['error'=>1];
        }
        return json($result);
    }
}